﻿using Microsoft.Extensions.Logging;
using System.Security.Cryptography;
using System.Xml.Linq;
using Week3_1_Entity;
using Week3_1_Entity.DTO;
using Week3_1_Entity.Enum;
using Week3_1_Repository;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Week3_1_Service
{
    public class OService
    {
        ORepository<UserInfo> useRep;
        ORepository<Goods> goodsRep;
        ORepository<Order> orderRep;
        ORepository<Address> addRep;
        ORepository<OrderGoods> ogRep;
        ORepository<SKU> skuRep;
        ORepository<GoodsSpec> gsRep;
        ORepository<GoodsSpecType> gstRep;
        ORepository<GoodsSpecGuan> gsgRep;
        ILogger<OService> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="useRep"></param>
        public OService(ORepository<UserInfo> useRep, ORepository<Goods> goodsRep, ORepository<Order> orderRep, ORepository<Address> addRep, ILogger<OService> logger, ORepository<OrderGoods> ogRep, ORepository<SKU> skuRep, ORepository<GoodsSpec> gsRep, ORepository<GoodsSpecType> gstRep, ORepository<GoodsSpecGuan> gsgRep)
        {
            this.useRep = useRep;
            this.goodsRep = goodsRep;
            this.orderRep = orderRep;
            this.addRep = addRep;
            this.logger = logger;
            this.ogRep = ogRep;
            this.skuRep = skuRep;
            this.gsRep = gsRep;
            this.gstRep = gstRep;
            this.gsgRep = gsgRep;
        }
        /// <summary>
        ///用户登录方法11
        /// </summary>
        /// <param name="Uname">用户名</param>
        /// <param name="Upwd">用户密码</param>
        /// <returns>返回受影响的实体</returns>
        public (int code,string msg,string uname,string upwd,int uid) GetUser(string Uname,string Upwd)
        {
            try
            {
                var list=useRep.GetAll().Where(x=>x.Uname==Uname).FirstOrDefault();
                if (list == null)
                {
                    return (-1, "用户名不存在", "", "", 0);
                }
                if(list.Upwd==Upwd)
                {
                    return (1, "登录成功", list.Uname, list.Upwd, list.Uid);
                }
                return (-2, "密码错误", "", "", 0);

            }
            catch (Exception ex)
            {
                logger.LogError("异常处理：返回用户登录方法出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示订单列表
        /// </summary>
        /// <param name="Uid"></param>
        /// <returns></returns>
        public List<OrderDTO> GetOrderDTO(int Uid)
        {
            try
            {
                var list = orderRep.GetAll().Where(x => x.Uid == Uid && x.IsDel == false);
                var dto = list.ToList().Select(x => new OrderDTO
                {
                    Oid = x.Oid,
                    OCode = x.OCode,
                    OsumPrice = x.OsumPrice,
                    OrderState = x.OrderState,
                    PayType = x.PayType,
                    Aname = x.Aname,
                    Adder = x.Adder,
                    Aphone = x.Aphone,
                    Uid = x.Uid,
                    ODate = x.ODate,
                    Lists = (from og in ogRep.GetAll()
                                 join g in goodsRep.GetAll()
                                 on og.GCode equals g.GCode
                                 where og.OCode==x.OCode
                                 select new OrderGoodsDTO
                                 {
                                     OGid=og.OGid,
                                     GCode=og.GCode,
                                     OCode=og.OCode,
                                     BuyCount =og.BuyCount,
                                     SumPrice=og.SumPrice,
                                     Uid=og.Uid,
                                     Gname =g.Gname,
                                     Gimg=g.Gimg
                                 }
                               ).ToList()
                });
                return dto.ToList();
            }
            catch (Exception ex)
            {
                logger.LogError("异常处理：返回显示订单商品列表出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示商品列表
        /// </summary>
        /// <returns></returns>
        public List<Goods> GetGoods()
        {
            try
            {
                var list = goodsRep.GetAll().ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示商品规格
        /// </summary>
        /// <param name="Gcode"></param>
        /// <returns></returns>
        public object GetGoodsSpec(string ?Gcode)
        {
            try
            {
                var list=skuRep.GetAll().Where(x=>x.GCode==Gcode).ToList();
                var sku = list.ToList().Select(x => new
                {
                    Scode=x.Scode,
                    GCode=x.GCode,
                    Stock=x.Stock,
                    Sname=x.Sname,
                    Svalue=x.Svalue,
                    Sdesc=x.Sdesc,
                    Simg=x.Simg,
                    SpecType=(from gst in gstRep.GetAll()
                              join gsg in gsgRep.GetAll()
                              on gst.GSTid equals gsg.GSTid
                              where gsg.GCode==Gcode
                              select new
                              {
                                  gst.GSTid,
                                  gst.GSTname,
                                  Spec=(from gs in gsRep.GetAll()
                                        where gs.GSTid==gst.GSTid
                                        select new
                                        {
                                            gs.GSid,
                                            gs.GSname
                                        }).ToList()
                              }).ToList()
                }).FirstOrDefault();
                return sku;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据SKU改变商品规格
        /// </summary>
        /// <param name="Gcode"></param>
        /// <param name="Svalue"></param>
        /// <returns></returns>
        public object GetSKU(string Gcode, string Svalue)
        {
            try
            {
                var list = skuRep.GetAll().Where(x => x.GCode == Gcode && x.Svalue== Svalue).ToList();
                var sku = list.ToList().Select(x => new
                {
                    Scode = x.Scode,
                    GCode = x.GCode,
                    Stock = x.Stock,
                    Sname = x.Sname,
                    Svalue = x.Svalue,
                    Sdesc = x.Sdesc,
                    Simg = x.Simg,
                    SpecType = (from gst in gstRep.GetAll()
                                join gsg in gsgRep.GetAll()
                                on gst.GSTid equals gsg.GSTid
                                where gsg.GCode == Gcode
                                select new
                                {
                                    gst.GSTid,
                                    gst.GSTname,
                                    Spec = (from gs in gsRep.GetAll()
                                            where gs.GSTid == gst.GSTid
                                            select new
                                            {
                                                gs.GSid,
                                                gs.GSname
                                            }).ToList()
                                }).ToList()
                }).FirstOrDefault();
                return sku;
            }
            catch (Exception)
            {

                throw;
            }
        }



























    }
}
